package com.arong.swing.db.dao;

import java.sql.SQLException;

import com.arong.swing.db.JDBCUtils;
import com.arong.swing.db.entity.AsAllGenerate;
import com.arong.swing.db.entity.Equipment;
import com.arong.swing.exception.AppException;
import com.arong.swing.exception.SysException;
import com.arong.swing.util.StringUtil;
public class EquipmentSDAO  extends BaseDAO {
	
	public EquipmentSDAO(){
		this(DB_ANRONGTEC);
	}
	
	public EquipmentSDAO(String dbFile){
		super(dbFile);
	}
	public EquipmentSDAO(JDBCUtils jdbcUtils){
		super(jdbcUtils);
	}
	public String getCreateTableSql(){
		return "CREATE TABLE IF NOT EXISTS "+
				"EQUIPMENT("+
				  "EQUIPT_ID VARCHAR2(32) PRIMARY KEY,"+
				  "IP_ADDR  VARCHAR2(32), "+
				  "DEV_CLASS_ID  NUMBER(12),"+
				  "EQUIPT_MODEL    VARCHAR2(32),"+
				  "PLATFORM_VN  VARCHAR2(32),"+
				  "PLATFORM_TYPE  VARCHAR2(32),"+
				  "NET_SURPORT  VARCHAR2(32),"+
				  "DEPT_ID   NUMBER(12),"+
				  "PHONE  VARCHAR2(32),"+
				  "EQUITB  VARCHAR2(128),"+
				  "MAC_ADDR  VARCHAR2(32),"+
				  "IMSI  VARCHAR2(32),"+
				  "USE_STATE  CHAR(1),"+
				  "BELONGUSER  VARCHAR2(32),"+
				  "USEDUSER  VARCHAR2(32),"+
				  "REGIST_TIME  DATE,"+
				  "STS_TIME  DATE,"+
				  "REMARK    VARCHAR2(256)"+
				")";
		
	}

	
	/**
	 * 插入
	 * @param entity
	 * @return
	 * @throws SysException, AppException
	 */
	public  int insert(Equipment entity) throws SysException,AppException {
		int i=0;
		try {
		 		    if (StringUtil.isBlank(entity.getEquiptId())) {
				entity.setEquiptId(getSeqValue());
			}
		 			i= jdbcUtils.insert(entity);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new SysException("add error", e);
		}
		return i;
	}

	/**
	 * 修改
	 * @param entity
	 * @return
	 * @throws SysException, AppException
	 */
	public  int update(Equipment entity) throws SysException,AppException {
		int i=0;
		try {
			i=jdbcUtils.update(entity, "equiptId");
		} catch (SQLException e) {
			throw new SysException("update error",e);
		}
		return i;
	}

	/**
	 * 删除
	 * @param entity
	 * @return
	 * @throws SysException, AppException
	 */
	public  int delete(String equiptId) throws SysException,AppException {
		int i = 0;
		try {
			i=jdbcUtils.execute("DELETE FROM EQUIPMENT WHERE EQUIPT_ID='"+equiptId+"'");
		} catch (SQLException e) {
			throw new SysException("delete error", e);
		}
		return i;
	}

	public  Equipment queryBeanByEuqID(String equiptId) throws SysException,AppException {
		Equipment res=null;
	    try {
			res=  jdbcUtils.queryBean(getSqlByEquId(equiptId), Equipment.class);
		} catch (SQLException e) {
			throw new SysException("queryBean对象查询 error",e);
		}
		return  res;
	}
	private String getSqlByEquId(String equiptId) {
		StringBuffer sql = new StringBuffer("SELECT ");
		sql.append(" a.EQUIPT_ID,a.IP_ADDR,a.DEV_CLASS_ID,a.EQUIPT_MODEL,a.PLATFORM_VN,a.PLATFORM_TYPE,a.NET_SURPORT,a.DEPT_ID,a.PHONE,a.EQUITB,a.MAC_ADDR,a.IMSI,a.USE_STATE,a.BELONGUSER,a.USEDUSER,a.REGIST_TIME,a.STS_TIME,a.REMARK");
		sql.append(" from EQUIPMENT a  where a.EQUIPT_ID ='"+equiptId+"'");

		return sql.toString();
	}
	public  Equipment getTop() throws SysException,AppException {
		Equipment res=null;
	    try {
	    	StringBuffer sql = new StringBuffer("SELECT ");
			sql.append(" a.EQUIPT_ID,a.IP_ADDR,a.DEV_CLASS_ID,a.EQUIPT_MODEL,a.PLATFORM_VN,a.PLATFORM_TYPE,a.NET_SURPORT,a.DEPT_ID,a.PHONE,a.EQUITB,a.MAC_ADDR,a.IMSI,a.USE_STATE,a.BELONGUSER,a.USEDUSER,a.REGIST_TIME,a.STS_TIME,a.REMARK");
			sql.append(" from EQUIPMENT a ORDER BY REGIST_TIME desc LIMIT 0,1");
			res=  jdbcUtils.queryBean(sql.toString(), Equipment.class);
		} catch (SQLException e) {
			throw new SysException("queryBean对象查询 error",e);
		}
		return   res;
	}
	public  long count() throws SysException,AppException {
		long i=0;
		try {
		 	  i= jdbcUtils.queryNumValue("select count(*) from EQUIPMENT");
		} catch (SQLException e) {
			throw new SysException("add error", e);
		}
		return i;
	}
}

